package com.sinog.front.controller.biz;

import com.sinog.core.model.UserDTO;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import com.sinog.front.config.log.Log;
import com.sinog.front.controller.biz.common.CommonDownloadController;
import com.sinog.front.feign.biz.AzbjDatabaseInfoService;
import com.sinog.front.service.user.UserFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @desc 安帮工作资源管理 - 安帮资料信息管理 - 帮教资料 - AzbjDatabaseInfoController
 * @author -
 * @date -
 */
@RestController
public class AzbjDatabaseInfoController {

    @Resource
    private AzbjDatabaseInfoService azbjDatabaseInfoService;
    @Autowired
    private UserFacade userService;
    @Resource
    private CommonDownloadController commonDownloadController;

    /**
     * 安帮工作资源管理 - 安帮资料信息管理 - 帮教资料 - 列表
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param querykey querykey
     * @return String
     */
    @RequestMapping("/azbj/abzlxxgl/azbjDatabaseInfo/list")
    public String getAzbjDatabaseList(Integer pageIndex,Integer pageSize,
                                      @RequestParam(required = false, defaultValue = "-1") String querykey) {
        UserDTO authentication = userService.getAuthentication();
        Map<String,Object> mapkey = new HashMap<>(16);
        mapkey.put("account",authentication.getAccount());
        mapkey.put("level",authentication.getLevel().substring(1));
        mapkey.put("querykey",querykey);
        mapkey.put("departId",authentication.getDepartid());
        return azbjDatabaseInfoService.getAzbjDatabaseList(pageIndex,pageSize,mapkey);
    }

    /**
     * 安帮工作资源管理 - 安帮资料信息管理 - 帮教资料 - （修改/新增）
     * @param request request
     * @param data data
     * @return Result
     */
    @Log(type = "新增", value = "保存帮教资料信息（修改/新增）")
    @PostMapping("/azbj/abzlxxgl/azbjDatabaseInfo/save")
    public Result saveAzbjDatabaseInfo(HttpServletRequest request,
                                       @RequestParam(required = false, defaultValue = "-1") String data) {
        return azbjDatabaseInfoService.saveAzbjDatabaseInfo(data);
    }

    /**
     * 安帮工作资源管理 - 安帮资料信息管理 - 帮教资料 - 下载帮教资料
     * @param request request
     * @param resp resp
     * @param id id
     * @param taskId taskId
     */
    @RequestMapping("/azbj/abzlxxgl/azbjDatabaseInfo/download")
    public void downloadAzbjDatabaseFile(HttpServletRequest request,HttpServletResponse resp,String id,String taskId) {
        //下载文件
        commonDownloadController.downloadFilesZip(resp,request,id,taskId);
        //记录次数
        azbjDatabaseInfoService.saveDownNum(id);
    }

    /**
     * 安帮工作资源管理 - 安帮资料信息管理 - 帮教资料 - 删除
     * @param request request
     * @param id id
     * @param taskId taskId
     * @return Result
     */
    @Log(type = "删除", value = "删除帮教资料信息")
    @DeleteMapping("/azbj/abzlxxgl/azbjDatabaseInfo/delete")
    public Result deleteAzbjDatabaseInfo(HttpServletRequest request,String id,String taskId) {
        //删除帮教资料表
        boolean bjzlresult = azbjDatabaseInfoService.deleteAzbjDatabaseInfo(id);
        //删除附件信息
        Map<String,Object> map = new HashMap<>(16);
        map.put("id",id);
        map.put("taskId",taskId);
        boolean fjresult = commonDownloadController.deleteFjInfo(map);
        if(bjzlresult && fjresult) {
            return Result.success("删除成功！");
        }
        return Result.failure(ReturnCode.DELETE_FAILED);
    }

    /**
     * 安帮工作资源管理 - 安帮资料信息管理 - 帮教资料 - 查询是否是自己发布的，编辑和删除只能操作自己发布的
     * @param id id
     * @return Result
     */
    @RequestMapping("/azbj/abzlxxgl/azbjDatabaseInfo/checkIfOwnerPublish/{id}")
    public Result checkIfOwnerPublish(@PathVariable String id) {
        Map<String,Object> map = new HashMap<>(16);
        map.put("account",userService.getAuthentication().getAccount());
        map.put("id",id);
        int count = azbjDatabaseInfoService.checkIfOwnerPublish(map);
        if(Constants.Number.NUM_ZERO < count) {
            return Result.success();
        } else {
            return Result.failure(ReturnCode.FAILURE);
        }
    }

    /**
     * 安帮工作资源管理 - 安帮资料信息管理 - 帮教资料 - 发布/取消发布
     * @param request request
     * @param id id
     * @param stateValue stateValue
     * @return Result
     */
    @Log(type = "更新", value = "发布/取消发布-帮教资料信息")
    @RequestMapping("/azbj/abzlxxgl/azbjDatabaseInfo/setfabuState")
    public Result setfabuState(HttpServletRequest request,String id,String stateValue) {
        return azbjDatabaseInfoService.setfabuState(id,stateValue);
    }
}